var app = getApp(), fetchApi = require("../../../utils/httpPost.js"),
 common_Js = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nick_name:'',
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
    isLoading:true,
    product_id:0,
    product:[],
    isShowPopup:false,
    productPrice:0,
    order_id:0,
    order:{},
    address:{},
    is_has_address:0,
    isVisit:0,//上需要上门的
    isAppointment:0,//开启预约的
    appointmentAddress:'',
    appointmentName:'',
    appointmentTime:'',
    appointmentPhone:'',
    isShowPopup:false,
    minDate: new Date().getTime(),
    remark:'',
    templateIds:[],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('paySubmit options', options)
    var order_id = parseInt(options.order_id)
    this.data.order_id = order_id;
    if(options.isAppointment){
      this.data.isAppointment = parseInt(options.isAppointment);
      this.setData({
        isAppointment:this.data.isAppointment
      })
    }
    if(options.isVisit){
      this.data.isVisit = parseInt(options.isVisit);
      this.setData({
        isVisit:this.data.isVisit
      })
    }
    this.onHttpInit(order_id)
  },
  onHttpInit: function (order_id) {
    var that = this
    fetchApi.httpPost('/product/order/detail', {
      order_id: order_id,
    }, function (res) {
      console.log("获取订单详情", res)
      if (res.data.code == 0) {
        that.data.coupons = res.data.data.order.coupons;
        that.data.order = res.data.data.order;
        if(res.data.data.order.orderType == 4){
          that.data.templateIds =  res.data.data.templateIds;
        }

        that.data.isAppointment =  res.data.data.order.isAppointment;
        that.setData({ 
          order: res.data.data.order,
          is_has_address: res.data.data.is_has_address,
          address:res.data.data.address,
          remark:res.data.data.order.remark,
        })
      }else{
        Toast.fail(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  onShow:function(options){
  },
  
  pushToAddress:function(){
    wx.navigateTo({
      url: "/pages/address/index?type=1"
    })
  },
  delInfo(e){
    console.log('delInfo',e);
    var infoId = e.currentTarget.dataset.infoid;
    var that = this;
    fetchApi.httpPost('/product/order/info/delete', {
      info_id: infoId,
    }, function (res) {
      console.log("删除详情", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg)
        setTimeout(function(){
          that.onHttpInit(that.data.order_id)
        },800)
      }else{
        Toast.fail(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  onChangeTime(e){
    console.log('onChangeTime',e)
    var date = new Date(e.detail);
    var time = common_Js.formatTime(date);
    console.log()
    this.setData({
      isShowPopup : false,
      appointmentTime:time
    })
  },
  onAppointmentTime(){
    this.setData({
      isShowPopup : !this.data.isShowPopup
    })
  },
  appointmentAddressHandle(e){
    this.data.appointmentAddress = e.detail;
    console.log('appointmentAddress',this.data.appointmentAddress)
  },
  appointmentPhoneHandle(e){
    this.data.appointmentPhone = e.detail;
    console.log('appointmentPhone',this.data.appointmentPhone)
  },
  appointmentNameHandle(e){
    this.data.appointmentName = e.detail;
    console.log('appointmentName',this.data.appointmentName)
  },
  msgHandle(e){
    this.data.remark = e.detail;
  },
  onCancelArea:function(){
    this.setData({
      isShowPopup:false,
    })
  },
  submitOrder:function(res) {
    var orderId = res.currentTarget.dataset.orderId;
    console.log('orderId',orderId);
    if(this.data.order.isAppointment == 1){
      if(this.data.appointmentName == ''){
        Toast('请输入联系人')
        return;
      }
      
      if(this.data.appointmentPhone == ''){
        Toast('请输入联系电话')
        return;
      }

      if(this.data.appointmentAddress == ''){
        Toast('请输入您的详细地址')
        return;
      }

    }
    this.requestPay(orderId);
  },
  requestPay:function(order_id){
    var that = this;
    fetchApi.httpPost('/pay/requestPay', {
      order_id: order_id,
      remark:that.data.remark,
      isAppointment:that.data.isAppointment,
      appointmentAddress:that.data.appointmentAddress,
      appointmentPhone:that.data.appointmentPhone,
      appointmentTime:that.data.appointmentTime,
      appointmentName:that.data.appointmentName,
      subject:'购买商家服务'
    }, function (res) {
      console.log('提交订单',res)
      if (res.data.code == 0) {
        var pay_data = res.data.data;
        var wxpayinfo = JSON.parse(res.data.data)
        console.log('pay_data',pay_data)
        if(wxpayinfo.result_code == 'SUCCESS'){
          wx.requestPayment({
            timeStamp: wxpayinfo.timeStamp,
            nonceStr: wxpayinfo.nonceStr,
            package: wxpayinfo.package,
            signType: wxpayinfo.signType,
            paySign: wxpayinfo.paySign,
            success (res) { 
              console.log('requestPayment success',res);
              if(that.data.order.orderType == 4  ){
                wx.requestSubscribeMessage({
                  tmplIds: that.data.templateIds,
                  success (res) {
                    console.log('requestSubscribeMessage',res)
                    wx.redirectTo({
                      url: '/pages/order/groupShare/index?order_id=' + order_id,
                    })
                  },fail(res){
                    wx.redirectTo({
                      url: '/pages/order/groupShare/index?order_id=' + order_id,
                    })
                  }
                })
              }else{
                wx.redirectTo({
                  url: '/pages/pay/payResult/index?order_id=' + order_id+'&type=warm',
                })
              }
            },
            fail (res) {
              console.log('requestPayment fail',res)
             }
          })
        }
      } else {
        Toast.fail(res.data.msg);
        that.setData({ isLoading: false, loading: true, netWork: true })
      }
    }, function () {
        that.setData({  isLoading: false,  loading: true,  netWork: true, }) 
    })
  },
})